package outputstream;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * FileOutputStream成员方法:
 *  public void write(byte[] b) throws IOException
 *      将字节数组中的所有字节写入文件中
 *  public void write(byte[] b, int off, int len)
 *      将字节数组中,从off索引开始,写入len个字节(包含off索引处的字节)
 *
 * String类中的方法:
 *  public byte[] getBytes():将字符串转使用平台默认码表(当前JDK 21 默认码表 UTF-8)换成字节数组
 */
public class FileOutputStreamDemo02 {
    public static void main(String[] args)throws Exception {
        //method01();
        //method02();
        //method03();
        FileOutputStream fos = new FileOutputStream("day22-Io-01/2.txt");
        byte[] bytes = {97, 98, 99, 100};//abcd
        fos.write(bytes, 1, 2);//从byte数组的1索引开始,写入两个字节
        fos.close();

    }

    private static void method03() throws IOException {
        FileOutputStream fos = new FileOutputStream("day22-Io-01/2.txt");
        byte[] bytes = "ABCD".getBytes();//查询平台默认码表UTF-8(所有码表兼容ASCII码表)=>65 66 67 68
        //将要写入文件的内容 转换成 字节数组
        fos.write(bytes);

        fos.close();
    }

    private static void method02() {
        System.out.println(System.getProperties());//System.getProperties() 获取系统的属性 获取系统的属性
        // ,其中有一个file.encoding=UTF-8
    }

    private static void method01() throws IOException {
        FileOutputStream fos = new FileOutputStream("day22-Io-01/2.txt");

        byte[] bytes = {65, 66, 67, 68};

        fos.write(bytes);//一次性通过字节数组写入四个字节

        fos.close();
    }
}
